<?php
class KolSys_Auth
{
	public $pwdhash="";
	private $loggedIn=false;
	private $isLoggedIn;
	private $isRollover;
	private $logInDelay;

	public static function getInstance()
	{
		return KolSys_Singleton::getInstance(__CLASS__);
	}

	public function isLoggedIn()
	{
		return $this->loggedIn;
	}

	public function getProperty($prop)
	{
		return $this->{$prop};
	}

	public function logOut()
	{
		//echo 'logging out...<br />';flush();
		$result=KolSys_Nav::getPage("logout.php");
		$this->loggedIn=false;
	}

	public function logIn($username=null,$password=null)
	{
		global $settings;
		if($username===null){$username=$settings['kol']['username'];}
		if($password===null){$password=$settings['kol']['password'];}
		//echo 'logging in...<br />';flush();
		$post=array('loginname'=>$username,'password'=>$password,'loggingin'=>'Yup.');
		$resultlog=KolSys_Nav::getPage("login.php",array('post'=>$post));
		//$resultlog=implode($resultlog);
		if(ereg("failures",$resultlog,$regs)){
			$this->loggedIn=false;
			die("Wait 15 minutes.");
		}else if(ereg("\>Whoops",$resultlog,$regs) || ereg("\>Too many login attempts in too short a span of time.  Please wait a minute \(Literally, like, one minute",$resultlog,$regs)){
			$this->loggedIn=false;
        			echo "Too many login attempts in too short a span of time.<br>\r\nTry again in a minute.";flush();
            			//sleep(65);
        			//$retries++;
            			//$this->logIn();
			exit;
	    	}
	    	if(ereg("Bad password.<",$resultlog,$regs)){$this->loggedIn=false;die("Wrong password.<br>\r\n");}
		$this->loggedIn=true;
    		return 1;
	}//method

	public function detectAuthChanges($request)
	{
		$bot=KolSys_Bot::getInstance();
		if(ereg("down for nightly maintenance",$response)){

		}else if(ereg("Whoops --",$response)){

		}else if(ereg("one minute",$response)){

		}else if(ereg("Not logged in",$response)){

		}else{
			return $response;
		}
	}

	public function get_pwdhash()
	{
		$pwdscreen=KolSys_Nav::getPage("store.php?whichstore=m");
		if(ereg("name=phash value=[\'\"]?([A-Za-z0-9]*)[\'\"]?",$pwdscreen,$regs))
		{
			$this->pwdhash=$regs[1];
			$this->loggedin=true;
	    	}else{
			$this->loggedin=false;
		}
    		return $this->pwdhash;
	}

	public function setLoggedIn($isLoggedIn)
	{
		$this->isLoggedIn=(bool)$isLoggedIn;
	}
	
	public function setRollover($isRollover)
	{
		$this->isRollover=(bool)$isRollover;
	}

	public function setLogInDelay($logInDelayInSeconds)
	{
		$this->logInDelay=(int)$logInDelayInSeconds;
	}

}//class